<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资产编号" prop="assetCode">
        <el-input
          v-model="queryParams.assetCode"
          placeholder="请输入资产编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产类型" prop="classify">
        <el-input
          v-model="queryParams.classify"
          placeholder="请输入资产类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产名称" prop="assetName">
        <el-input
          v-model="queryParams.assetName"
          placeholder="请输入资产名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价值" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始日期" prop="usedDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.usedDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择开始使用日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label=" 取得日期" prop="acquireDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.acquireDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择取得日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label=" 原值" prop="originalValue">
        <el-input
          v-model="queryParams.originalValue"
          placeholder="请输入 原值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理部门" prop="manageDepartmentId">
        <el-input
          v-model="queryParams.manageDepartmentId"
          placeholder="请输入管理部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="ownerDepartmentId">
        <el-input
          v-model="queryParams.ownerDepartmentId"
          placeholder="请输入所属部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用部门" prop="usedDepartmentId">
        <el-input
          v-model="queryParams.usedDepartmentId"
          placeholder="请输入使用部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用年限" prop="serviceLife">
        <el-input
          v-model="queryParams.serviceLife"
          placeholder="请输入使用年限"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input
          v-model="queryParams.memo"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理人" prop="managerId">
        <el-input
          v-model="queryParams.managerId"
          placeholder="请输入管理人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用人" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入使用人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label=" 创建时间" prop="createDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择 创建日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['common:asset:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['eam:asset:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['eam:asset:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['eam:asset:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="资产编号" align="center" prop="assetCode"/>
      <el-table-column label="资产类型" align="center" prop="classify"/>
      <el-table-column label="资产名称" align="center" prop="assetName"/>
      <el-table-column label="价值" align="center" prop="price"/>
      <el-table-column label="开始使用日期" align="center" prop="usedDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.usedDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="取得日期" align="center" prop="acquireDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acquireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="取得方式" align="center" prop="acquireType"/>
      <el-table-column label="原值" align="center" prop="originalValue"/>
      <el-table-column label="管理部门" align="center" prop="manageDepartmentId"/>
      <el-table-column label="所属部门" align="center" prop="ownerDepartmentId"/>
      <el-table-column label="使用部门" align="center" prop="usedDepartmentId"/>
      <el-table-column label="使用年限" align="center" prop="serviceLife"/>
      <el-table-column label="备注" align="center" prop="memo"/>
      <el-table-column label="管理人" align="center" prop="managerId"/>
      <el-table-column label="使用人" align="center" prop="userId"/>
      <el-table-column label=" 创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label=" 修改时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['eam:asset:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['eam:asset:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="资产编号" prop="assetCode">
              <el-input v-model="form.assetCode" placeholder="请输入资产编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产名称" prop="assetName">
              <el-input v-model="form.assetName" placeholder="请输入 资产名称"/>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="原值" prop="originalValue">
              <el-input type="number" v-model="form.originalValue" placeholder="请输入原值"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价值" prop="price">
              <el-input type="number" v-model="form.price" placeholder="请输入价值"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="资产类型" prop="classify">
              <el-input v-model="form.classify" placeholder="请输入资产类型"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用年限" prop="serviceLife">
              <el-input type="number" v-model="form.serviceLife" placeholder="请输入使用年限"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="管理人" prop="managerId">
              <el-input v-model="form.managerId" placeholder="请输入管理人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用人" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入使用人"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="开始使用日期" prop="usedDate">
              <el-date-picker clearable
                              v-model="form.usedDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择开始使用日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="取得日期" prop="acquireDate">
              <el-date-picker clearable
                              v-model="form.acquireDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择 取得日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="管理部门" prop="manageDepartmentId">
              <treeselect v-model="form.manageDepartmentId" :options="deptOptions" :show-count="true"
                          placeholder="请选择管理部门"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属部门" prop="ownerDepartmentId">
              <treeselect v-model="form.ownerDepartmentId" :options="deptOptions" :show-count="true" placeholder="请选择所属部门"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="使用部门" prop="usedDepartmentId">
            <treeselect v-model="form.departmentId" :options="deptOptions" :show-count="true" placeholder="请选择使用部门"/>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="memo">
            <el-input v-model="form.memo" placeholder="请输入备注"/>
          </el-form-item>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAsset, getAsset, delAsset, addAsset, updateAsset} from "@/api/eam/asset";
import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: "Asset",
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产表格数据
      assetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetCode: null,
        classify: null,
        assetName: null,
        price: null,
        usedDate: null,
        acquireDate: null,
        acquireType: null,
        originalValue: null,
        manageDepartmentId: null,
        ownerDepartmentId: null,
        usedDepartmentId: null,
        serviceLife: null,
        memo: null,
        managerId: null,
        userId: null,
        createTime: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询资产列表 */
    getList() {
      this.loading = true;
      listAsset(this.queryParams).then(response => {
        this.assetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        assetId: null,
        assetCode: null,
        classify: null,
        assetName: null,
        price: null,
        usedDate: null,
        acquireDate: null,
        acquireType: null,
        originalValue: null,
        manageDepartmentId: null,
        ownerDepartmentId: null,
        usedDepartmentId: null,
        serviceLife: null,
        memo: null,
        managerId: null,
        userId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.assetId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资产";
      this.getTreeselect();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      const assetId = row.assetId || this.ids
      getAsset(assetId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资产";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.assetId != null) {
            updateAsset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAsset(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const assetIds = row.assetId || this.ids;
      this.$modal.confirm('是否确认删除资产编号为"' + assetIds + '"的数据项？').then(function () {
        return delAsset(assetIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('common/asset/export', {
        ...this.queryParams
      }, `asset_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
